package com.qwt.part_time_api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qwt.part_time_api.error.BusinessException;
import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.pojo.PersonalInformation;
import com.qwt.part_time_api.pojo.User;
import com.qwt.part_time_api.service.*;
import com.qwt.part_time_api.utils.JodaTimeUtil;
import com.qwt.part_time_api.vo.Result;
import com.qwt.part_time_api.vo.params.RegisterParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author 君子慎独
 * @create 2022/1/21 0021 0:37
 */
@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {

    /**
     * 静态变量存储最大值
     */
    private final AtomicInteger atomicNum = new AtomicInteger();
    @Autowired
    private UserService userService;
    @Autowired
    private PersonalInformationService p;
    @Autowired
    private RoleService roleService;
    @Autowired
    private NumberService numberService;

    @Override
    public Result saveUser(RegisterParams params) throws BusinessException {
        log.info("注册用户");
        Long uid;
        int maxGroupNum;
        String newAutoNum = null;
        if (StringUtils.isEmpty(params.getAccount())) {
            log.info("账号为空");
            throw new BusinessException(ErrorCode.USER_ACCOUNT_NOT_NULL);
        }

        // 查询用户是否已经注册
        uid = userService.getUserIdByAccount(params.getAccount());
        if (Objects.nonNull(uid)) {
            log.info("用户已经注册");
            throw new BusinessException(ErrorCode.USER_ACCOUNT_ALREADY_EXIST);
        }

        User user = new User();
        user.setAccount(params.getAccount());
        user.setPassword("$2a$10$8vQfcIAnX3LhAAGW57l2UO1g151PnebS4caUqCNQJeJsNlyY7Nfoi");
        user.setCreateTime(JodaTimeUtil.date2Str(new Date()));
        int insert = userService.saveUser(user);
        if (insert == 1) {
            uid = userService.getUserIdByAccount(params.getAccount());
            // 添加信息主表
            PersonalInformation information = new PersonalInformation();
            information.setUid(uid);
            maxGroupNum = numberService.findMax();
            atomicNum.set(maxGroupNum);
            newAutoNum = getNewAutoNum();
            information.setName("用户" + newAutoNum);
            information.setAge(18);
            information.setSex("男");
            information.setEmail(params.getAccount());
            information.setType(0L);
            information.setAvatars("https://2580.oss-cn-shenzhen.aliyuncs.com/image/%E7%8C%AB%E5%A4%B4%E9%B9%B0%E5%8D%9A%E5%A3%AB%E5%A4%B4%E5%83%8F%EF%BC%8C%E5%8D%A1%E9%80%9A%E5%8A%A8%E7%89%A9.png");
            information.setPraise(100);
            information.setStatus(0);
            p.savePersonalInformation(information);
            //    添加角色 刚注册用户默认是游客权限
            roleService.saveUserRole(3L, uid);
        }
        log.info("注册成功");
        assert newAutoNum != null;
        numberService.updateMax(Integer.parseInt(newAutoNum));
        return Result.success(null);
    }

    /**
     * 自增编号
     */
    public String getNewAutoNum() {
        //线程安全的原子操作，所以此方法无需同步
        int newNum = atomicNum.incrementAndGet();
        //数字长度为5位，长度不够数字前面补0
        return String.format("%06d", newNum);
    }

}
